package com.rado.tf.gui.view.userList.impl;

import com.rado.tf.domain.User;
import com.rado.tf.gui.i18n.MessageSource;
import com.rado.tf.gui.view.common.impl.AbstractViewImpl;
import com.rado.tf.gui.view.userList.IUserListView;
import com.vaadin.data.fieldgroup.FieldGroup;
import com.vaadin.data.util.BeanItem;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.data.validator.BeanValidator;
import com.vaadin.ui.*;
import com.vaadin.ui.themes.ChameleonTheme;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

/**
 * Created by parado on 18.03.14.
 */
@org.springframework.stereotype.Component(UserListViewImpl.BEAN_NAME)
public class UserListViewImpl extends AbstractViewImpl implements IUserListView
{
   public static final String BEAN_NAME = "userListView";

   @Resource
   private MessageSource messageSource;

   private BeanItemContainer<User> bc;

   private Table table;

   private FieldGroup fieldGroup;

   @Override
   @PostConstruct
   public void init()
   {
      setSpacing(true);
      setSizeFull();

      VerticalLayout master = new VerticalLayout();
      master.setSpacing(true);
      VerticalLayout detail = new VerticalLayout();
      detail.setMargin(true);
      detail.setSpacing(true);
      VerticalSplitPanel splitPanel = new VerticalSplitPanel(master, detail);
      splitPanel.setStyleName(ChameleonTheme.SPLITPANEL_SMALL);
      addComponent(splitPanel);

      master.addComponent(buildTableControls());
      master.addComponent(buildTable());
      detail.addComponent(buildForm());
      detail.addComponent(buildFormControls());
   }

   private Component buildTableControls()
   {
      HorizontalLayout tableControls = new HorizontalLayout();
      tableControls.setSpacing(true);
      tableControls.setMargin(true);
      Button add = new Button(messageSource.getMessage("new.button"));
      Button delete = new Button(messageSource.getMessage("delete.button"));
      tableControls.addComponent(add);
      tableControls.addComponent(delete);
      return tableControls;
   }

   private Component buildForm()
   {
      fieldGroup = new FieldGroup();
      GridLayout form = new GridLayout(2, 3);
      form.setSpacing(true);

      TextField firstName = new TextField(messageSource.getMessage("first.name.label"));
      firstName.setNullRepresentation(StringUtils.EMPTY);
      firstName.addValidator(new BeanValidator(User.class, User.FIELD_FIRST_NAME));
      firstName.setImmediate(true);

      TextField lastName = new TextField(messageSource.getMessage("last.name.label"));
      lastName.setNullRepresentation(StringUtils.EMPTY);
      lastName.addValidator(new BeanValidator(User.class, User.FIELD_LAST_NAME));
      lastName.setImmediate(true);

      TextField email = new TextField(messageSource.getMessage("email.label"));
      email.setNullRepresentation(StringUtils.EMPTY);
      email.addValidator(new BeanValidator(User.class, User.FIELD_EMAIL));
      email.setImmediate(true);

      fieldGroup.bind(firstName, User.FIELD_FIRST_NAME);
      fieldGroup.bind(lastName, User.FIELD_LAST_NAME);
      fieldGroup.bind(email, User.FIELD_EMAIL);

      form.addComponent(firstName);
      form.addComponent(lastName);
      form.addComponent(email);
      return form;
   }

   private Component buildTable()
   {
      table = new Table();
      table.setSizeFull();
      table.setSelectable(true);
      table.setImmediate(true);
      table.setStyleName(ChameleonTheme.TABLE_BORDERLESS);

      bc = new BeanItemContainer<User>(User.class);
      table.setContainerDataSource(bc);
      table.setVisibleColumns(new String[]{User.FIELD_FIRST_NAME, User.FIELD_LAST_NAME, User.FIELD_EMAIL});
      table.setColumnHeader(User.FIELD_FIRST_NAME, messageSource.getMessage("first.name.label"));
      table.setColumnHeader(User.FIELD_LAST_NAME, messageSource.getMessage("last.name.label"));
      table.setColumnHeader(User.FIELD_EMAIL, messageSource.getMessage("email.label"));

      return table;
   }

   @Override
   public void editUser(User user)
   {
      if (user == null)
      {
         user = new User();
      }
      BeanItem<User> item = new BeanItem<User>(user);
      fieldGroup.setItemDataSource(item);
   }

   @Override
   public Table getTable()
   {
      return table;
   }

   private Component buildFormControls()
   {
      HorizontalLayout formControls = new HorizontalLayout();
      formControls.setSpacing(true);
      Button save = new Button(messageSource.getMessage("save.button"));
      Button discard = new Button(messageSource.getMessage("discard.button"));
      formControls.addComponent(save);
      formControls.addComponent(discard);
      return formControls;
   }

   @Override
   public void setUserList(List<User> users)
   {
      bc.removeAllItems();
      bc.addAll(users);
   }
}
